package com.nf.servlet;

import com.nf.dao.StudentDao;
import com.nf.dao.StudentsDaoImpl;
import com.nf.entity.Students;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应编码
        response.setContentType("application/json;charset=utf-8");
        //获取客户端参数
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        int age = Integer.parseInt(request.getParameter("age"));
        Date birthday = null;
        try {
            birthday = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birthday"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Students students = new Students(name,sex,age,birthday);
        //调用dao操作数据库添加数据
        StudentDao dao = new StudentsDaoImpl();
        int re = dao.addStudnt(students);
        //重定向回客户端
        response.sendRedirect("index.html");
    }
}
